package com.ocom.common.utils.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.utils.Base64Util;
import com.ocom.common.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
public class WxGhUtils {

    //token 获取
    public static String getAccessToken(String appid, String secret) {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
        Map<String, Object> objectMap = HttpUtils.doGet(url);
        log.info("GzhMsg 获取token  【{}】 【{}】【{}】",appid,secret,objectMap);
        if (!objectMap.containsKey("access_token")) return null;
        return (String) objectMap.get("access_token");
    }


    //获取票据
    public static String getSnTicket(String accessToken, String modelId, String sn) {
        try {
            String url = "https://api.weixin.qq.com/wxa/getsnticket?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("model_id", modelId);
            jsonObject.put("sn", sn);
            String s = HttpUtils.doPostBodyData(url, jsonObject, "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(s);
            if (jsonObject1.getInteger("errcode") == 0) {
                return jsonObject1.getString("sn_ticket");
            }
            log.info("获取snTicket异常1 【{}】【{}】", sn, s);
            return null;
        } catch (Exception exception) {
            log.error("获取snTicket异常2 【{}】【{}】", sn, exception);
            return null;
        }
    }

    //addDevice
    public static String addDevice(String accessToken, Integer productId, String modelName, List<String> deviceIdList) {
        try {
            String url = "https://api.weixin.qq.com/wxa/business/runtime/adddevice?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("product_id", productId);
            jsonObject.put("device_id_list", deviceIdList);
            jsonObject.put("model_name", modelName);
            String s = HttpUtils.doPostBodyData(url, jsonObject, "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(s);
            if (jsonObject1.getInteger("errcode") == 0) {
                return jsonObject1.getString("errmsg");
            }
            log.info("获取addDevice异常1 【{}】【{}】", "addDevice", s);
            return null;
        } catch (Exception exception) {
            log.error("获取addDevice异常2 【{}】【{}】", "addDevice", exception);
            return null;
        }
    }


    //创建设备组
    public static String createIotGroupId(String accessToken, String modelId, String groupName) {
        try {
            String url = "https://api.weixin.qq.com/wxa/business/group/createid?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("model_id", modelId);
            jsonObject.put("group_name", groupName);
            String s = HttpUtils.doPostBodyData(url, jsonObject, "utf-8");
            JSONObject jsonObject1 = JSON.parseObject(s);
            if (jsonObject1.getInteger("errcode") == 0) {
                return jsonObject1.getString("group_id");
            }
            log.info("createIotGroupId异常1 【{}】【{}】", groupName, s);
            return null;
        } catch (Exception exception) {
            log.error("createIotGroupId异常2 【{}】【{}】", groupName, exception);
            return null;
        }
    }

    //设备组添加设备
    public static String addIotGroupDevice(String accessToken, String modelId, String groupId, String sn) {
        try {
            String url = "https://api.weixin.qq.com/wxa/business/group/adddevice?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("group_id", groupId);
            List<JSONObject> list = new ArrayList<>();
            JSONObject jsonObject31 = new JSONObject();
            jsonObject31.put("model_id", modelId);
            jsonObject31.put("sn", sn);
            list.add(jsonObject31);
            jsonObject.put("device_list", list);
            jsonObject.put("force_add",true);//是否强制更新设备列表，等于 true 时将已存在其它设备组中的设备移除并添加到当前设备组
            String s = HttpUtils.doPostBodyData(url, jsonObject, "utf-8");
            return s;
        } catch (Exception exception) {
            log.error("addIotGroupDevice异常2 【{}】【{}】", sn, exception);
            return null;
        }
    }

    //设备组删除设备
    public static String removeIotGroupDevice(String accessToken, String modelId, String groupId, String sn) {
        try {
            String url = "https://api.weixin.qq.com/wxa/business/group/removedevice?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("group_id", groupId);
            List<JSONObject> list = new ArrayList<>();
            JSONObject jsonObject31 = new JSONObject();
            jsonObject31.put("model_id", modelId);
            jsonObject31.put("sn", sn);
            list.add(jsonObject31);
            jsonObject.put("device_list", list);
            String s = HttpUtils.doPostBodyData(url, jsonObject, "utf-8");
            return s;
        } catch (Exception exception) {
            log.error("removeIotGroupDevice异常2 【{}】【{}】", sn, exception);
            return null;
        }
    }


    //激活设备license
    public static String activeDevice(String accessToken, Integer pkgType, List<JSONObject> deviceIdList) {
        try {
            String url = "https://api.weixin.qq.com/wxa/business/license/activedevice?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pkg_type", pkgType);
            jsonObject.put("device_list", deviceIdList);
            String s = HttpUtils.doPostBodyData(url, jsonObject, "utf-8");
            return s;
        } catch (Exception exception) {
            log.error("activedevice异常2 【{}】【{}】", deviceIdList, exception);
            return null;
        }
    }


    public static void main(String[] args) {
        String ty = WxGhUtils.getAccessToken("wx89c7e162346dec80", "c92358433c6ae040eecca27013187b90");
//        String yu = getSnTicket(ty,"B1BD7QO-0cUqk2H4g6u8WQ","69305332");
//        String yu = createIotGroupId(ty,"B1BD7QO-0cUqk2H4g6u8WQ","海南华侨中学视频电话");
        String yu = addIotGroupDevice(ty, "B1BD7QO-0cUqk2H4g6u8WQ", "gva8R-V0TF0JLjbGHDUdb_A", "69305332");
        System.out.println(ty);
        System.out.println(yu);
    }

    //生成二维码
    public static String getUnlimited(String accessToken, String page, String scene) throws IOException {
        JSONObject jsonObject = new JSONObject();
        InputStream content = null;
        try {
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
            jsonObject.put("scene", scene);
            if (StringUtils.isNotBlank(page)) {
                jsonObject.put("page", page);
            }
            byte[] bytes = HttpUtils.doPostBodyEntity(url, jsonObject, "utf-8");

            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            BufferedImage bufferedImage = ImageIO.read(in);

            return Base64Util.BufferedImageToBase64(bufferedImage);
        } catch (Exception exception) {
            log.error("getUnlimited异常 【{}】【{}】", exception, jsonObject);
            return null;
        } finally {
            if (null != content) {
                content.close();
            }
        }
    }

}
